fastlane_version "2.129.0"

default_platform :ios

platform :ios do


  # Documentation ==============

  desc "Build Documentation"
  lane :documentation do
    docc_web
  end


  # Lint =======================

  desc "Run SwiftLint"
  lane :lint do
    swiftlint(strict: true)
  end


  # Test =======================
  
  desc "Run unit tests"
  lane :test do
    sh("swift test")
  end

  
  # Version ====================

  desc "Create a new version"
  lane :version do |options|
    ensure_git_status_clean
    ensure_git_branch(branch: 'main')
    
    lint
    test
    docc_web
    
    bump_type = options[:type]
    version = version_bump_podspec(
      path: "StoreKitPlus.podspec",
      bump_type: bump_type)
  
    git_commit(path: "*", message: "Bump to #{version}")
    add_git_tag(tag: version)
    push_git_tags()
    push_to_git_remote()
    pod_push()
  end


  # Docs =======================
  
  desc "Build documentation for all platforms"
  lane :docc do
    sh('cd .. && rm -rf Docs')
    docc_platform(destination: 'iOS', name: 'ios')
    docc_platform(destination: 'OS X', name: 'osx')
    docc_platform(destination: 'tvOS', name: 'tvos')
    docc_platform(destination: 'watchOS', name: 'watchos')
  end
    
  desc "Build documentation for a single platform"
  lane :docc_platform do |values|
    sh('cd .. && mkdir -p Docs')
    docc_delete_derived_data
    sh('cd .. && xcodebuild docbuild \
      -scheme StoreKitPlus \
      -destination \'generic/platform=' + values[:destination] + '\'')
    sh('cd .. && \
      find ~/Library/Developer/Xcode/DerivedData \
      -name "StoreKitPlus.doccarchive" \
      -exec cp -R {} Docs \;')
    sh('cd .. && \
      mv Docs/StoreKitPlus.doccarchive Docs/StoreKitPlus_' + values[:name] + '.doccarchive')
  end
  
  desc "Delete documentation derived data (may be historic duplicates)"
  lane :docc_delete_derived_data do
    sh('find ~/Library/Developer/Xcode/DerivedData \
      -name "StoreKitPlus.doccarchive" \
      -exec rm -Rf {} \; || true')
  end
    
  desc "Build static documentation websites for all platforms"
  lane :docc_web do
    docc
    docc_web_platform(name: 'ios')
    docc_web_platform(name: 'osx')
    docc_web_platform(name: 'tvos')
    docc_web_platform(name: 'watchos')
  end
    
  desc "Build static documentation website for a single platform"
  lane :docc_web_platform do |values|
    sh('cd .. && $(xcrun --find docc) process-archive \
      transform-for-static-hosting Docs/StoreKitPlus_' + values[:name] + '.doccarchive \
      --output-path Docs/web_' + values[:name] + ' \
      --hosting-base-path StoreKitPlus')
  end
  
end
